package it.univaq.di.helpdesk.presentation;

import it.univaq.di.helpdesk.common.utils.MimeTypeImgHelper;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoadMimeImgAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		response.setContentType("image/png");
		
		String mimeType = (String) request.getParameter("mimeType");
		
		MimeTypeImgHelper mimeimg = MimeTypeImgHelper.getInstance();
		
		String imgPath = getServlet().getServletContext().getRealPath(mimeimg.getIconPath(mimeType));
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(imgPath));
		
		ServletOutputStream sos = response.getOutputStream();
		
		byte [] imgData = new byte[128];
		
		while (bis.read(imgData) != -1) {
			sos.write(imgData);
		}
		
		sos.flush();
		bis.close();
		sos.close();
		return null;
	}

}
